package com.cyh.testcustomui;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.TextView;


import android.annotation.SuppressLint;

import androidx.annotation.Nullable;

@SuppressLint("AppCompatCustomView")

/**
 * 继承textView可以忽略测量过程
 * */
public class ColorTrackTextView extends TextView {
    private static final String TAG = "ColorTrackTextView";

    private int mOriginColor = Color.BLACK;
    private int mChangeColor = Color.RED;

    private Paint mOriginPaint;
    private Paint mChangePaint;

    private float mCurrentProgress = 0.3f;

    private Direction mDirection = Direction.RIGHT_TO_LEFT;
    public enum Direction{
        LEFT_TO_RIGHT, RIGHT_TO_LEFT
    }

    public ColorTrackTextView(Context context) {
        this(context, null);
    }

    public ColorTrackTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ColorTrackTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ColorTrackTextView);
        mOriginColor = array.getColor(R.styleable.ColorTrackTextView_originColor, mOriginColor);
        mChangeColor = array.getColor(R.styleable.ColorTrackTextView_changeColor, mChangeColor);
        array.recycle();

        mOriginPaint = new Paint();
        mOriginPaint.setColor(mOriginColor);
        mOriginPaint.setAntiAlias(true); //抗锯齿
        mOriginPaint.setDither(true);//防抖动
        mOriginPaint.setTextSize(getTextSize());

        mChangePaint = new Paint();
        mChangePaint.setColor(mChangeColor);
        mChangePaint.setAntiAlias(true); //抗锯齿
        mChangePaint.setDither(true);//防抖动
        mChangePaint.setTextSize(getTextSize());
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //super.onDraw(canvas); 不能用系统的，要自己画

        //获取变色的位置
        int middle = (int)(mCurrentProgress*getWidth());
        if(mDirection == Direction.LEFT_TO_RIGHT) {
            //绘制原色
            drawText(canvas, mChangePaint, 0, middle);
            //绘制变色
            drawText(canvas, mOriginPaint, middle, getWidth());
        } else {
            //绘制原色
            drawText(canvas, mChangePaint, getWidth() - middle, getWidth());
            //绘制变色
            drawText(canvas, mOriginPaint, 0, getWidth() - middle);
        }

        /*String text = getText().toString();
        //计算基线
        Rect textBounds = new Rect();
        mOriginPaint.getTextBounds(text, 0, text.length(), textBounds);
        int x = getWidth()/2 - textBounds.width()/2;
        Paint.FontMetricsInt fontMetrics = mOriginPaint.getFontMetricsInt();
        int dy = (fontMetrics.bottom - fontMetrics.top)/2 - fontMetrics.bottom;
        int baseLine = getHeight()/2 + dy; //中线 + dy


        canvas.save();


        //绘制原色 （可以先机器区域再绘制文字， 也可以先绘制出文字然后再截取区域）

        //截取区域
        Rect rect = new Rect(0, 0, middle, getHeight());
        canvas.clipRect(rect);

        //绘制原色文字

        canvas.drawText(text, x, baseLine, mOriginPaint); // 这其实绘制的是左半部分的原色。 原因是canvas调用了clipRect截取了区域。


        canvas.restore();

        canvas.save();

        //绘制变色
        rect = new Rect(middle, 0, getWidth(), getHeight());
        canvas.clipRect(rect);
        canvas.drawText(text, x, baseLine, mChangePaint);

        canvas.restore();*/

    }

    private void drawText(Canvas canvas, Paint paint, int start, int end) {
        String text = getText().toString();
        //计算基线
        Rect textBounds = new Rect();
        paint.getTextBounds(text, 0, text.length(), textBounds);
        int x = getWidth()/2 - textBounds.width()/2;
        Paint.FontMetricsInt fontMetrics = paint.getFontMetricsInt();
        int dy = (fontMetrics.bottom - fontMetrics.top)/2 - fontMetrics.bottom;
        int baseLine = getHeight()/2 + dy; //中线 + dy


        canvas.save();


        //绘制原色 （可以先机器区域再绘制文字， 也可以先绘制出文字然后再截取区域）

        //截取区域
        Rect rect = new Rect(start, 0, end, getHeight());
        canvas.clipRect(rect);

        //绘制原色文字

        canvas.drawText(text, x, baseLine, paint); // 这其实绘制的是左半部分的原色。 原因是canvas调用了clipRect截取了区域。


        canvas.restore();
    }
}
